package com.ruoyi.wechat.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import com.ruoyi.wechat.domain.Appointment;
import com.ruoyi.wechat.mapper.AppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.DetailsMapper;
import com.ruoyi.wechat.domain.Details;
import com.ruoyi.wechat.service.IDetailsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 预约详情Service业务层处理
 * 
 * @author mu
 * @date 2024-11-20
 */
@Service
public class DetailsServiceImpl implements IDetailsService 
{
    @Resource
    private DetailsMapper detailsMapper;

    /**
     * 查询预约详情
     * 
     * @param detailId 预约详情主键
     * @return 预约详情
     */
    @Override
    public Details selectDetailsByDetailId(String detailId)
    {
        return detailsMapper.selectDetailsByDetailId(detailId);
    }

    /**
     * 查询预约详情列表
     * 
     * @param details 预约详情
     * @return 预约详情
     */
    @Override
    public List<Details> selectDetailsList(Details details)
    {
        return detailsMapper.selectDetailsList(details);
    }

    @Resource
    private AppointmentMapper appointmentMapper;
    /**
     * 新增预约详情
     * 
     * @param details 预约详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDetails(Details details)
    {
        //给详情id赋值
        details.setDetailId(UUID.randomUUID().toString());
        //查询预约
        Appointment appointment = appointmentMapper.selectAppointmentByAppointId(details.getAppointId());
        //判断金额
        if (appointment.getAmount()==null){
            appointment.setAmount(details.getUnitPrice());
        }else{
            appointment.setAmount(appointment.getAmount().add(details.getUnitPrice()));
        }
        //更新预约金额
        appointmentMapper.updateAppointment(appointment);
        //新增预约详情
        return detailsMapper.insertDetails(details);
    }

    /**
     * 修改预约详情
     * 
     * @param details 预约详情
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDetails(Details details)
    {
        //更新详情
        //detailsMapper.updateDetails(details);
        //创建预约
        Appointment appointment = new Appointment();
        appointment.setAppointId(details.getAppointId());

        //查询所有详情
        Details detailsQuery = new Details();
        detailsQuery.setAppointId(details.getAppointId());
        List<Details> detailsList = detailsMapper.selectDetailsList(detailsQuery);
        if (detailsList!=null && detailsList.size()>0){
            detailsList.forEach(d -> {
                if (appointment.getAmount()==null){
                    appointment.setAmount(d.getUnitPrice());
                }else{
                    appointment.getAmount().add(d.getUnitPrice());
                }
            });
        }
        //更新预约金额
        appointmentMapper.updateAppointment(appointment);
        //更新预约详情
        return detailsMapper.updateDetails(details);
    }

    /**
     * 批量删除预约详情
     * 
     * @param detailIds 需要删除的预约详情主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDetailsByDetailIds(String[] detailIds)
    {
        //循环更新预约金额
        for (int i = 0; i < detailIds.length; i++) {
            //更新预约的总金额
            Details details = detailsMapper.selectDetailsByDetailId(detailIds[i]);
            Appointment appointment = appointmentMapper.selectAppointmentByAppointId(details.getAppointId());
            if (appointment.getAmount() == null){
                appointment.setAmount(new BigDecimal(0));
            }else{
                appointment.setAmount(appointment.getAmount().subtract(details.getUnitPrice()));
            }
            appointmentMapper.updateAppointment(appointment);
        }
        //删除预约详情
        return detailsMapper.deleteDetailsByDetailIds(detailIds);
    }

    /**
     * 删除预约详情信息
     * 
     * @param detailId 预约详情主键
     * @return 结果
     */
    @Override
    public int deleteDetailsByDetailId(String detailId)
    {
        //更新预约的总金额
        Details details = detailsMapper.selectDetailsByDetailId(detailId);
        Appointment appointment = appointmentMapper.selectAppointmentByAppointId(details.getAppointId());
        if (appointment.getAmount() == null){
            appointment.setAmount(new BigDecimal(0));
        }else{
            appointment.getAmount().subtract(details.getUnitPrice());
        }
        appointmentMapper.updateAppointment(appointment);
        //删除预约详情
        return detailsMapper.deleteDetailsByDetailId(detailId);
    }
}










